﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Red.Controls;
using Red.Controls.DataDriven;
using Red.Controls.Shell;
using Red.Controls.ViewModels;

namespace Red.Toolkit.WorldEditor
{
    /// <summary>
    /// Interaction logic for SceneTreeNode.xaml
    /// </summary>
    [InteropControl( "WorldTreeNode" )]
    public partial class SceneTreeNode : UserControl
    {
        private static Canvas _nodeIcon;

        private bool _isVisible = true;

        static SceneTreeNode()
        {
            _nodeIcon = RedApplication.FindCanvasResource( "appbar_cards_diamond" );
        }

        public SceneTreeNode()
        {
            InitializeComponent();
            DataContextChanged += OnDataContextChanged;
        }

        private void OnDataContextChanged( object sender, DependencyPropertyChangedEventArgs e )
        {
            InteropTreeItem treeItem = e.NewValue as InteropTreeItem;

            if ( treeItem == null )
                return;

            treeItem.Image = _nodeIcon;

            List<RedMenuDataItem> menu = new List<RedMenuDataItem>();
            menu = WorldEditorCommands.NodeItemCommands.Select( command => new RedMenuDataItem( new CommandWrapper( command, treeItem ) ) ).ToList();
            treeItem.SetMenu( menu );
        }

        private void ToggleVisibility_Executed( object sender, ExecutedRoutedEventArgs e )
        {
            InteropTreeItem treeItem = e.Parameter as InteropTreeItem;

            if ( treeItem == null )
                return;

            _isVisible = !_isVisible;

            treeItem.Execute( "setVisibility", _isVisible );
        }
    }

}
